package vip.xiaonuo.smzq.modular.dictionary.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import vip.xiaonuo.common.pojo.CommonResult;
import vip.xiaonuo.smzq.core.gson.GsonUtil;
import vip.xiaonuo.smzq.core.tools.excel.ExcelImportUtil;
import vip.xiaonuo.smzq.modular.dictionary.entity.Cb14Kcp;
import vip.xiaonuo.smzq.modular.dictionary.entity.excelEnt.Cb14KcpExc;
import vip.xiaonuo.smzq.modular.dictionary.entity.vo.DirTree;
import vip.xiaonuo.smzq.modular.dictionary.entity.vo.KcNameTree;
import vip.xiaonuo.smzq.modular.dictionary.service.ICb14KcpService;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;


/**
 * 矿产品
 *
 * @author eomer
 * @since 2024-02-21
 */
@Api(tags = "矿产品")
@RestController
@RequestMapping("/dictionary/cb14Kcp")
@Validated
public class Cb14KcpController {

    @Resource
    private ICb14KcpService iCb14KcpService;

    /**
     * 添加矿产品
     */
    @ApiOperation("添加矿产品")
    @PostMapping("/add")
    public CommonResult<String> add(@RequestBody @Valid Cb14Kcp cb14Kcp) {
        iCb14KcpService.save(cb14Kcp);
        return CommonResult.ok();
    }

    /**
     * 删除矿产品
     */
    @ApiOperation("删除矿产品")
    @PostMapping("/delete")
    public CommonResult<String> delete(@ApiParam(name = "pkid", value = "主键") @RequestParam String pkid) {
        iCb14KcpService.removeById(pkid);
        return CommonResult.ok();
    }

    /**
     * 编辑矿产品
     */
    @ApiOperation("编辑矿产品")
    @PostMapping("/edit")
    public CommonResult<String> edit(@RequestBody @Valid Cb14Kcp cb14Kcp) {
        iCb14KcpService.updateById(cb14Kcp);
        return CommonResult.ok();
    }

    /**
     * 矿产品分页查询
     */
    @ApiOperation("矿产品分页查询")
    @GetMapping("/list")
    public CommonResult<Page<Cb14Kcp>> page() {
        return CommonResult.data(iCb14KcpService.page(new Cb14Kcp()));
    }

    /**
     * 矿产品下拉树
     */
//    @ApiOperation("矿产品下拉树")
//    @GetMapping("/getKcpDirTree")
//    public CommonResult<KcNameTree> getKcpDirTree() {
//        return CommonResult.data(iCb14KcpService.getKcpDirTree());
//    }

    /**
     * 矿产品下拉树-重构
     *
     * @author Kevin
     * @since 2024/7/23 16:47
     */
    @ApiOperation("矿产品下拉树-重构")
    @GetMapping("/getKcpDirTree")
    public CommonResult<KcNameTree> getKcpTree() {
        return CommonResult.data(iCb14KcpService.getKcpTree());
    }

    /**
     * 导入
     */
    @ApiOperation("导入")
    @PostMapping("/import")
    public CommonResult<String> importExcel(@RequestParam("file") MultipartFile file) {
        try {
            List<Cb14KcpExc> list = ExcelImportUtil.readMultipartFile(file, Cb14KcpExc.class);
            List<Cb14Kcp> cb01Zhs = GsonUtil.ObjectToList(list, Cb14Kcp.class);
            iCb14KcpService.saveBatch(cb01Zhs);
            return CommonResult.ok();
        } catch (Exception e) {
            return CommonResult.error(e.getMessage());
        }
    }
}
